{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bf290ecc-577c-4fb5-8e1d-ffebfefaac9e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-25T15:24:52.293048300Z",
     "start_time": "2024-08-25T15:24:51.266966200Z"
    }
   },
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: '../爬虫/read_test.csv'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mFileNotFoundError\u001B[0m                         Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[1], line 2\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mpandas\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m \u001B[38;5;21;01mpd\u001B[39;00m\n\u001B[1;32m----> 2\u001B[0m df\u001B[38;5;241m=\u001B[39m\u001B[43mpd\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mread_csv\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43m../爬虫/read_test.csv\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m)\u001B[49m\n\u001B[0;32m      3\u001B[0m df\n",
      "File \u001B[1;32md:\\三创\\python\\lib\\site-packages\\pandas\\io\\parsers\\readers.py:1026\u001B[0m, in \u001B[0;36mread_csv\u001B[1;34m(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, date_format, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options, dtype_backend)\u001B[0m\n\u001B[0;32m   1013\u001B[0m kwds_defaults \u001B[38;5;241m=\u001B[39m _refine_defaults_read(\n\u001B[0;32m   1014\u001B[0m     dialect,\n\u001B[0;32m   1015\u001B[0m     delimiter,\n\u001B[1;32m   (...)\u001B[0m\n\u001B[0;32m   1022\u001B[0m     dtype_backend\u001B[38;5;241m=\u001B[39mdtype_backend,\n\u001B[0;32m   1023\u001B[0m )\n\u001B[0;32m   1024\u001B[0m kwds\u001B[38;5;241m.\u001B[39mupdate(kwds_defaults)\n\u001B[1;32m-> 1026\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[43m_read\u001B[49m\u001B[43m(\u001B[49m\u001B[43mfilepath_or_buffer\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mkwds\u001B[49m\u001B[43m)\u001B[49m\n",
      "File \u001B[1;32md:\\三创\\python\\lib\\site-packages\\pandas\\io\\parsers\\readers.py:620\u001B[0m, in \u001B[0;36m_read\u001B[1;34m(filepath_or_buffer, kwds)\u001B[0m\n\u001B[0;32m    617\u001B[0m _validate_names(kwds\u001B[38;5;241m.\u001B[39mget(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mnames\u001B[39m\u001B[38;5;124m\"\u001B[39m, \u001B[38;5;28;01mNone\u001B[39;00m))\n\u001B[0;32m    619\u001B[0m \u001B[38;5;66;03m# Create the parser.\u001B[39;00m\n\u001B[1;32m--> 620\u001B[0m parser \u001B[38;5;241m=\u001B[39m TextFileReader(filepath_or_buffer, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwds)\n\u001B[0;32m    622\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m chunksize \u001B[38;5;129;01mor\u001B[39;00m iterator:\n\u001B[0;32m    623\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m parser\n",
      "File \u001B[1;32md:\\三创\\python\\lib\\site-packages\\pandas\\io\\parsers\\readers.py:1620\u001B[0m, in \u001B[0;36mTextFileReader.__init__\u001B[1;34m(self, f, engine, **kwds)\u001B[0m\n\u001B[0;32m   1617\u001B[0m     \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39moptions[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mhas_index_names\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m kwds[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mhas_index_names\u001B[39m\u001B[38;5;124m\"\u001B[39m]\n\u001B[0;32m   1619\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mhandles: IOHandles \u001B[38;5;241m|\u001B[39m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;01mNone\u001B[39;00m\n\u001B[1;32m-> 1620\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_engine \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_make_engine\u001B[49m\u001B[43m(\u001B[49m\u001B[43mf\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mengine\u001B[49m\u001B[43m)\u001B[49m\n",
      "File \u001B[1;32md:\\三创\\python\\lib\\site-packages\\pandas\\io\\parsers\\readers.py:1880\u001B[0m, in \u001B[0;36mTextFileReader._make_engine\u001B[1;34m(self, f, engine)\u001B[0m\n\u001B[0;32m   1878\u001B[0m     \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mb\u001B[39m\u001B[38;5;124m\"\u001B[39m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;129;01min\u001B[39;00m mode:\n\u001B[0;32m   1879\u001B[0m         mode \u001B[38;5;241m+\u001B[39m\u001B[38;5;241m=\u001B[39m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mb\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[1;32m-> 1880\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mhandles \u001B[38;5;241m=\u001B[39m \u001B[43mget_handle\u001B[49m\u001B[43m(\u001B[49m\n\u001B[0;32m   1881\u001B[0m \u001B[43m    \u001B[49m\u001B[43mf\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m   1882\u001B[0m \u001B[43m    \u001B[49m\u001B[43mmode\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m   1883\u001B[0m \u001B[43m    \u001B[49m\u001B[43mencoding\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43moptions\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mget\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mencoding\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;28;43;01mNone\u001B[39;49;00m\u001B[43m)\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m   1884\u001B[0m \u001B[43m    \u001B[49m\u001B[43mcompression\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43moptions\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mget\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mcompression\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;28;43;01mNone\u001B[39;49;00m\u001B[43m)\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m   1885\u001B[0m \u001B[43m    \u001B[49m\u001B[43mmemory_map\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43moptions\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mget\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mmemory_map\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;28;43;01mFalse\u001B[39;49;00m\u001B[43m)\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m   1886\u001B[0m \u001B[43m    \u001B[49m\u001B[43mis_text\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mis_text\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m   1887\u001B[0m \u001B[43m    \u001B[49m\u001B[43merrors\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43moptions\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mget\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mencoding_errors\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mstrict\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m)\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m   1888\u001B[0m \u001B[43m    \u001B[49m\u001B[43mstorage_options\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43moptions\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mget\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mstorage_options\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;28;43;01mNone\u001B[39;49;00m\u001B[43m)\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m   1889\u001B[0m \u001B[43m\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m   1890\u001B[0m \u001B[38;5;28;01massert\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mhandles \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m\n\u001B[0;32m   1891\u001B[0m f \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mhandles\u001B[38;5;241m.\u001B[39mhandle\n",
      "File \u001B[1;32md:\\三创\\python\\lib\\site-packages\\pandas\\io\\common.py:873\u001B[0m, in \u001B[0;36mget_handle\u001B[1;34m(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)\u001B[0m\n\u001B[0;32m    868\u001B[0m \u001B[38;5;28;01melif\u001B[39;00m \u001B[38;5;28misinstance\u001B[39m(handle, \u001B[38;5;28mstr\u001B[39m):\n\u001B[0;32m    869\u001B[0m     \u001B[38;5;66;03m# Check whether the filename is to be opened in binary mode.\u001B[39;00m\n\u001B[0;32m    870\u001B[0m     \u001B[38;5;66;03m# Binary mode does not support 'encoding' and 'newline'.\u001B[39;00m\n\u001B[0;32m    871\u001B[0m     \u001B[38;5;28;01mif\u001B[39;00m ioargs\u001B[38;5;241m.\u001B[39mencoding \u001B[38;5;129;01mand\u001B[39;00m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mb\u001B[39m\u001B[38;5;124m\"\u001B[39m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;129;01min\u001B[39;00m ioargs\u001B[38;5;241m.\u001B[39mmode:\n\u001B[0;32m    872\u001B[0m         \u001B[38;5;66;03m# Encoding\u001B[39;00m\n\u001B[1;32m--> 873\u001B[0m         handle \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mopen\u001B[39;49m\u001B[43m(\u001B[49m\n\u001B[0;32m    874\u001B[0m \u001B[43m            \u001B[49m\u001B[43mhandle\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m    875\u001B[0m \u001B[43m            \u001B[49m\u001B[43mioargs\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mmode\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m    876\u001B[0m \u001B[43m            \u001B[49m\u001B[43mencoding\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mioargs\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mencoding\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m    877\u001B[0m \u001B[43m            \u001B[49m\u001B[43merrors\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43merrors\u001B[49m\u001B[43m,\u001B[49m\n\u001B[0;32m    878\u001B[0m \u001B[43m            \u001B[49m\u001B[43mnewline\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m,\u001B[49m\n\u001B[0;32m    879\u001B[0m \u001B[43m        \u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m    880\u001B[0m     \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[0;32m    881\u001B[0m         \u001B[38;5;66;03m# Binary mode\u001B[39;00m\n\u001B[0;32m    882\u001B[0m         handle \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mopen\u001B[39m(handle, ioargs\u001B[38;5;241m.\u001B[39mmode)\n",
      "\u001B[1;31mFileNotFoundError\u001B[0m: [Errno 2] No such file or directory: '../爬虫/read_test.csv'"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df=pd.read_csv(\"../爬虫/read_test.csv\")\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c0910b8-5170-4a5f-9d06-26aecf92c125",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-25T15:24:52.299030900Z",
     "start_time": "2024-08-25T15:24:52.297036400Z"
    }
   },
   "outputs": [],
   "source": [
    "#map:对Serier做数据映射(一一对应)\n",
    "#apply:对Series的值做数据处理,对DataFrame的Series做数据处理\n",
    "#applymap:只能处理DataFrame的数据\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "011c7ea1-eae2-459d-8dba-a4a11eff213c",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.300028400Z"
    }
   },
   "outputs": [],
   "source": [
    "#利用匿名函数实现转换:uv*1.5\n",
    "df[\"活跃用户数\"]=df[\"uv\"].map(lambda x:x*1.5)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f33fe21-c6c5-4033-ac9a-786620d8bd80",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.303020500Z"
    }
   },
   "outputs": [],
   "source": [
    "利用\n",
    "df[\"地区\"]=df[\"prov\"].map(lambda (hunan:\"湖南\",hubei:\"湖北\"))\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "128f4a84-8713-4f73-90e6-288a259d39ac",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-25T15:24:52.315985700Z",
     "start_time": "2024-08-25T15:24:52.305014800Z"
    }
   },
   "outputs": [],
   "source": [
    "dict_provs={\n",
    "    \"hunan\":\"湖南\",\n",
    "    \"hubei\":\"湖北\"\n",
    "}\n",
    "df[\"省份\"]=df[\"prov\"].map(lambda x:dict_provs[x])\n",
    "df\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba3aa6da-786c-4124-b985-b9c78e3f0c96",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.308007600Z"
    }
   },
   "outputs": [],
   "source": [
    "#apply=>适用于series和dataframe\n",
    "#series.apply(function)\n",
    "#dataframe.apply(function)\n",
    "\n",
    "df[\"省份2\"]=df[\"prov\"].apply(lambda x:dict_provs[x])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c15128fb-4bad-4293-8f36-c74a552dba06",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.310001900Z"
    }
   },
   "outputs": [],
   "source": [
    "df[['pv','uv']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cdea3772-ab46-4583-b3a7-98cb97173eb5",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.312993500Z"
    }
   },
   "outputs": [],
   "source": [
    "#x-->一行数据\n",
    "#默认按列求和\n",
    "df[['pv','uv']].apply(lambda x:x.sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79091aa1-48ed-4932-acb9-9a8551645ffe",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.315985700Z"
    }
   },
   "outputs": [],
   "source": [
    "#axis=1 =>跨列操作\n",
    "df[\"总计\"]=df[['pv','uv']].apply(lambda x:x.sum(),axis=1)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a817933c-1fdf-491a-8291-b8ad32d5ed3e",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.318977900Z"
    }
   },
   "outputs": [],
   "source": [
    "df[['pv','uv']].map(lambda x:int(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c03dc471-23ea-4763-b9f7-c01106cb1e24",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-25T15:24:52.353886Z",
     "start_time": "2024-08-25T15:24:52.321969900Z"
    }
   },
   "outputs": [],
   "source": [
    "df=pd.read_excel(\"../爬虫/stu_data.xlsx\")\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "054e465c-031e-4aa5-8e1e-3339c29d03b3",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.324962200Z"
    }
   },
   "outputs": [],
   "source": [
    "stu_sex = {\n",
    "    \"男\":\"男\",\n",
    "    \"女\":\"女\",\n",
    "    1:\"男\",\n",
    "    0:\"女\"\n",
    "}\n",
    "df[\"性别\"]=df[\"性别\"].map(stu_sex)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "489aa476-6220-4679-afaa-a737658e5d24",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.326956400Z"
    }
   },
   "outputs": [],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a84564c-71c8-4111-9cb4-f0a8f2fdfaa4",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.330948Z"
    }
   },
   "outputs": [],
   "source": [
    "def stu_hei(x):\n",
    "    if x.endswith(\"cm\"):\n",
    "        return x\n",
    "    else:\n",
    "        value = float(x.strip(\"m\"))*100\n",
    "        return f\"{int(value)}cm\"\n",
    "       \n",
    "df[\"身高\"]=df.iloc[0:6,2].apply(stu_hei)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2731d6f-50d3-4141-9167-8b64e46d3b31",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.332942100Z"
    }
   },
   "outputs": [],
   "source": [
    "df[\"总分\"]=df[[\"语文\",\"数学\",\"英语\"]].apply(lambda x:x.sum(),axis=1)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aeccfa25-fbed-4406-a062-f2b93d5930e2",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.335934100Z"
    }
   },
   "outputs": [],
   "source": [
    "df.loc[\"平均分\"]=df[[\"语文\",\"数学\",\"英语\"]].apply(lambda x: int(x.mean()))\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea90e8b3-d1ec-4a96-bd90-98f358d1402c",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.339922Z"
    }
   },
   "outputs": [],
   "source": [
    "#北京天气数据\n",
    "df = pd.read_csv(\"../爬虫/beijing_tianqi_2018.csv\")\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b4c3c65c-b5b8-4fa5-aa90-914aaee464d2",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.342913500Z"
    }
   },
   "outputs": [],
   "source": [
    "#去掉温度的单位\n",
    "df[\"bWendu\"]=df[\"bWendu\"].str.replace(\"℃\",\"\").astype(\"int32\")\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b413ae6-e6e9-49aa-8bfc-13a81da95de6",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.345906500Z"
    }
   },
   "outputs": [],
   "source": [
    "df[\"yWendu\"]=df[\"yWendu\"].str.replace(\"℃\",\"\").astype(\"int32\")\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4a35202e-d0a9-41e5-aadc-22d5cd6461c6",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.348898600Z"
    }
   },
   "outputs": [],
   "source": [
    "#增加一类设置天气类型\n",
    "#bWendu>35-高温\n",
    "#bWendu<=35,0>常温\n",
    "#bWendu<=0 低温\n",
    "# df[\"bWendu\"].apply(get_wendu_type)\n",
    "# Serise => get_wendu_type 得到的参数是Serise中的一个值\n",
    "def get_wendu_type(x):\n",
    "    if x > 35:\n",
    "        return \"高温\"\n",
    "    elif x > 0:\n",
    "        return \"常温\"\n",
    "    else:\n",
    "        return \"低温\"\n",
    "\n",
    "df[\"天气类型1\"] = df[\"bWendu\"].apply(get_wendu_type)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8d24d678-2442-4671-978d-c8e68e969ab6",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.350894300Z"
    }
   },
   "outputs": [],
   "source": [
    "#统计每种不同天气的出现次数\n",
    "df[\"天气类型1\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "effbcbc8-d7d9-424b-91b4-56dee27c5639",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.352888700Z"
    }
   },
   "outputs": [],
   "source": [
    "# df.apply(get_wendu_type)\n",
    "# DataFrame => get_wendu_type 得到的参数一个Serise\n",
    "\n",
    "def get_wendu_type2(x):\n",
    "#     print(x)\n",
    "    if x[\"bWendu\"]>35:\n",
    "        return \"高温\"\n",
    "    elif x[\"bWendu\"]>0:\n",
    "        return \"常温\"\n",
    "    else:\n",
    "        return \"低温\"\n",
    "\n",
    "# 注意：dataframe.apply默认以列为单位处理，使用axis=1改为以行为单位处理\n",
    "df[\"天气类型2\"] = df.apply(get_wendu_type2, axis=1)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "029341cd-33a5-49b2-9345-ebeafde6efbe",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-25T15:24:52.374828800Z",
     "start_time": "2024-08-25T15:24:52.355881400Z"
    }
   },
   "outputs": [],
   "source": [
    "#1.df.apply(func)\n",
    "#2.\n",
    "#创建一个新列,空字符串\n",
    "df[\"wencha_type\"]=\"\"\n",
    "df.loc[df[\"bWendu\"]-df[\"yWendu\"]>=10,\"wencha_type\"]=\"温差大\"\n",
    "df.loc[df[\"bWendu\"]-df[\"yWendu\"]<=10,\"wencha_type\"]=\"温差小\"\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3caf2c36-a812-408c-bcf9-86c16b93fd9b",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.357875100Z"
    }
   },
   "outputs": [],
   "source": [
    "df[\"wencha_type\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2bece853-5042-48cd-88d2-95b155ab440f",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.358872500Z"
    }
   },
   "outputs": [],
   "source": [
    "df.loc[:,\"wencha\"]=df[\"bWendu\"]-df[\"yWendu\"]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c281a93e-8322-4bde-9fd2-e03631706f16",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.360867600Z"
    }
   },
   "outputs": [],
   "source": [
    "df.assign(\n",
    "    yWendu_huashi=lambda x:x[\"yWendu\"]*9/5+32,\n",
    "    bWendu_huashi=lambda x:x[\"bWendu\"]*9/5+32\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11546acc-f438-49f8-9378-88e49b5d0e67",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.362862100Z"
    }
   },
   "outputs": [],
   "source": [
    "#对缺少值的处理\n",
    "#isnull=>是否为空  True\n",
    "#notnull=>是否不为空 True\n",
    "#dropna=>丢弃缺失值\n",
    "#fillna=>\n",
    "\n",
    "std_df=pd.read_excel(\"../爬虫/student_excel.xlsx\")\n",
    "std_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "40f112dc-974d-45d9-af64-d4124b1dd1e8",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.363859600Z"
    }
   },
   "outputs": [],
   "source": [
    "std_df.isnull()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c00abe7d-4891-4e9b-b165-650bc300821d",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.364856500Z"
    }
   },
   "outputs": [],
   "source": [
    "std_df.notnull()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0c6d25cd-8cdf-49f4-b62d-138a3124c43e",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.366851300Z"
    }
   },
   "outputs": [],
   "source": [
    "std_df[\"姓名\"].notnull()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b944dae8-0821-44cd-899c-b3944aa4369c",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.367849400Z"
    }
   },
   "outputs": [],
   "source": [
    "std_df[0:3].notnull()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9d3a281-5e16-4e0a-beca-c4aaed9bea20",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.369843300Z"
    }
   },
   "outputs": [],
   "source": [
    "std_df.loc[std_df[\"分数\"].notnull()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0269990c-411e-45ff-b874-ea04ac3515af",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.370840200Z"
    }
   },
   "outputs": [],
   "source": [
    "#删除全是空值的列\n",
    "#0 or index /1 or columns\n",
    "#how:any 任何一个值为空,all 所有值为空\n",
    "std_df.dropna(axis=\"columns\",how=\"all\",inplace=True)\n",
    "std_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "06e911ac-2d52-41fb-9e31-2dcbde2703c0",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.372835400Z"
    }
   },
   "outputs": [],
   "source": [
    "#删除全为空的行\n",
    "std_df.dropna(axis=\"index\",how=\"all\",inplace=True)\n",
    "std_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "040fb951-10c0-497d-9e9b-2946b2325583",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.373849700Z"
    }
   },
   "outputs": [],
   "source": [
    "#将分数为Nan的数据填充为o\n",
    "#直接再std_df上做修改\n",
    "std_df.fillna({\"分数\":0})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "49c78229-dd79-41ee-b9eb-75694997aa7a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-25T15:24:52.385799100Z",
     "start_time": "2024-08-25T15:24:52.375826800Z"
    }
   },
   "outputs": [],
   "source": [
    "std_df.loc[:,\"分数\"]=std_df[\"分数\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2658e2a2-19fe-40b6-9e42-4507b81c8db7",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.376840200Z"
    }
   },
   "outputs": [],
   "source": [
    "#ffill=> forward fill 向后填充\n",
    "# bfill=>先后填充\n",
    "std_df[\"姓名\"]=std_df[\"姓名\"].ffill()\n",
    "std_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "172f493f-9cea-4b06-a979-852ef4c07d68",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.378818100Z"
    }
   },
   "outputs": [],
   "source": [
    "std_df.to_excel(\"../爬虫/student_excel_clean.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10722e2a-35dc-4436-a832-ce8aa5c63f0f",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.380812700Z"
    }
   },
   "outputs": [],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03711a42-bddc-4eb9-aa6f-1cdd9433a6f5",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.381809400Z"
    }
   },
   "outputs": [],
   "source": [
    "df.sort_values(by=[\"tianqi\",\"aqi\"],ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c2684fa-27a7-476b-be3a-32cfc4228478",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.383804200Z"
    }
   },
   "outputs": [],
   "source": [
    "df.sort_values(by=[\"tianqi\",\"aqi\"],ascending=[True,False])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c4ddb4b-20e3-42f0-957d-a06b144e9db3",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.384801500Z"
    }
   },
   "outputs": [],
   "source": [
    "# 新增一列：month\n",
    "df[\"month\"] = df[\"ymd\"].str[:7]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e5f19c9-31c4-468b-9228-f9f3891c0096",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.386796Z"
    }
   },
   "outputs": [],
   "source": [
    "# count => 计数\n",
    "# 统计每个月有多少条数据\n",
    "df.groupby(\"month\")[\"month\"].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0cfee342-6012-4b01-82d2-8646e0d139aa",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-08-25T15:24:52.403786900Z",
     "start_time": "2024-08-25T15:24:52.387793200Z"
    }
   },
   "outputs": [],
   "source": [
    "# 统计每个月的最高温度\n",
    "df.groupby(\"month\")[\"bWendu\"].max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc3dccc9-0f4b-4424-9a3f-b09d28f98e99",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.389787700Z"
    }
   },
   "outputs": [],
   "source": [
    "# 统计每个月的最底温度\n",
    "data=df.groupby(\"month\")[\"yWendu\"].min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37b7797d-ee4d-444d-983c-0be3237b3bee",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.390785200Z"
    }
   },
   "outputs": [],
   "source": [
    "type(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9aa54123-1d13-4d9f-8797-482b4675fa8a",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-08-25T15:24:52.392808900Z"
    }
   },
   "outputs": [],
   "source": [
    "import matplotlib\n",
    "data.plot"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
